﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Model
{
    /// <summary>
    /// 时间帮助类（不理解）
    /// </summary>
    public static class TimeHelper
    {
        private static readonly long epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks;

        /// <summary>
        /// 客户端当前时间
        /// </summary>
        /// <returns></returns>
        public static long ClientNow()
        {
            return (DateTime.UtcNow.Ticks - epoch) / 10000;
        }

        public static long ClientNowSeconds()
        {
            return (DateTime.UtcNow.Ticks - epoch) / 10000000;
        }

        /// <summary>
		/// 登陆前是客户端时间,登陆后是同步过的服务器时间
		/// </summary>
		/// <returns></returns>
		public static long Now()
        {
            return ClientNow();
        }


        /// <summary>
        /// 判断两日期是否相等，忽略日期。只比较时间。
        /// </summary>
        /// <param name="d1"></param>
        /// <param name="d2"></param>
        /// <returns></returns>
        public static bool Is(this DateTime d1, DateTime d2)
        {
            int s1 = d1.Hour * 60 * 60 + d1.Minute * 60 + d1.Second;
            int s2 = d2.Hour * 60 * 60 + d2.Minute * 60 + d2.Second;
            return s1 == s2;
        }

        /// <summary>
        /// 大于
        /// </summary>
        /// <param name="d1"></param>
        /// <param name="d2"></param>
        /// <returns></returns>
        public static bool GT(this DateTime d1, DateTime d2)
        {
            int s1 = (d1.Hour * 60 * 60) + (d1.Minute * 60) + d1.Second;
            int s2 = (d2.Hour * 60 * 60) + (d2.Minute * 60) + d2.Second;
            return s1 > s2;
        }

        /// <summary>
        /// 小于
        /// </summary>
        /// <param name="d1"></param>
        /// <param name="d2"></param>
        /// <returns></returns>
        public static bool LT(this DateTime d1, DateTime d2)
        {
            int s1 = d1.Hour * 60 * 60 + d1.Minute * 60 + d1.Second;
            int s2 = d2.Hour * 60 * 60 + d2.Minute * 60 + d2.Second;
            return s1 < s2;
        }

        /// <summary>
        /// 生成指定次数的时间列表
        /// </summary>
        /// <param name="startTime"></param>
        public static Queue<DateTime> GeneratorTimes(DateTime startTime, int m, int count)
        {
            Queue<DateTime> queue = new Queue<DateTime>();
            queue.Enqueue(startTime);
            var nextTime = startTime;
            var i = 0;
            while (true)
            {
                nextTime = nextTime.AddMilliseconds(m);
                queue.Enqueue(nextTime);
                ++i;
                if (i == count)
                    break;
            }
            return queue;
        }

        /// <summary>
        /// 生成范围间隔时间列表
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="s"></param>
        public static Queue<DateTime> GeneratorTimes(DateTime startTime, DateTime endTime, int m)
        {
            Queue<DateTime> queue = new Queue<DateTime>();
            queue.Enqueue(startTime);
            var nextTime = startTime;
            while (true)
            {
                nextTime = nextTime.AddMilliseconds(m);
                queue.Enqueue(nextTime);
                if (nextTime.Is(endTime))
                {
                    break;
                }
            }
            return queue;
        }
    }
}
